package main

import (
    "roy/common/api"
)

type Person struct {
    Username, Password, Nickname string
}

type District struct {
    Code, Name string
}

type AdminLog struct {
    id, username, loginTime, loginIp, action, log, ctime string
}

func main() {

    //region json 字符串转数组
    //str := `[{"code":"123123","name":"roy"}]`
    //ss := make([]map[string]interface{}, 0)
    //err := json.Unmarshal([]byte(str), &ss)
    //if err != nil {
    //    panic(err)
    //}
    ////fmt.Println(ss)
    //j, er := json.Marshal(ss)
    //if er != nil {
    //    panic(er)
    //}
    //fmt.Println(string(j))
    //endregion

    //region 查询单条数据
    //var p Person
    //var sqlStr1 = "select username,password,nickname from yunduan_admin_user"
    //row := data.GetRow(sqlStr1)
    //err := row.Scan(&p.Username, &p.Password, &p.Nickname)
    //if err != nil {
    //    panic(err)
    //}
    //fmt.Printf("username: %s password: %s nickname: %s\n", p.Username, p.Password, p.Nickname)
    // endregion

    //region 查询数据库集合 并将结果集转为 json
    //sqlStr2 := "select code,name from yunduan_district where code like '11%'"
    //rows := data.GetRows(sqlStr2)
    //arr := make([]map[string]string, 0)
    //for rows.Next() {
    //    d := make(map[string]string)
    //    code := ""
    //    name := ""
    //    rows.Scan(&code, &name)
    //    d["code"] = code
    //    d["name"] = name
    //    arr = append(arr, d)
    //}
    //str, _ := json.Marshal(arr)
    //fmt.Println(string(str))
    //endregion

    //region 将数据集转为数组
    //sqlStr2 := "select code,name from yunduan_district where code like '11%'"
    //rows := data.GetRows(sqlStr2)
    //strArr, err := data.Rows2Array(rows)
    //if err != nil {
    //    fmt.Println(err)
    //}
    //fmt.Println(strArr)
    //endregion

    //region 将数组转为 json
    //sqlStr2 := "select code,name from yunduan_district where code like '11%'"
    //rows := data.GetRows(sqlStr2)
    //strArr, err := data.Rows2Array(rows)
    //if err != nil {
    //    panic(err)
    //}
    //jsonStr := data.Array2Json(strArr)
    //fmt.Println(jsonStr)
    //endregion

    //region 数据集转为数组再转为 json
    //sqlStr2 := "select code,name from yunduan_district where code like '11%'"
    //rows := data.GetRows(sqlStr2)
    //var arr2 []interface{}
    //var jsonArr []string
    //for rows.Next() {
    //    var code string
    //    var name string
    //    rows.Scan(&code, &name)
    //    s := make(map[string]string)
    //    s["code"] = code
    //    s["name"] = name
    //    arr2 = append(arr2, s)
    //    str, _ := json.Marshal(s)
    //    jsonArr = append(jsonArr, string(str))
    //}
    //jsonStr, err := json.Marshal(arr2)
    //if err != nil {
    //    panic(err)
    //}
    //fmt.Println(arr2)
    //fmt.Println(jsonArr)
    //fmt.Println(string(jsonStr))
    // endregion

    //region 操作 file
    //content := file.GetContent("./roy.txt")
    //for _, s := range content {
    //    fmt.Printf(s)
    //}
    //w := "heqiang"
    //s := file.PutContent("./roy.txt", w)
    //if s {
    //    fmt.Println("追加写入成功")
    //}
    //endregion

    //region 接口
    //request.Main1()
    api.Api()
    //endregion

    //region 测试遍历二维数组
    //arr := make([]map[string]interface{}, 0)
    //for i := 0; i < 5; i++ {
    //    item := make(map[string]interface{})
    //    item["key"+strconv.Itoa(i)] = "value" + strconv.Itoa(i)
    //    arr = append(arr, item)
    //}
    //fmt.Println(arr)
    //for i, m := range arr {
    //    fmt.Println(i, m)
    //}
    //jsonStr, _ := json.Marshal(arr)
    //fmt.Println(string(jsonStr))
    //endregion

    //region es 测试
    // 创建 es 索引
    //flag := es.CreateIndex("hehehe")
    //fmt.Println(flag)
    // 判断 es 索引是否存在
    //flag := es.IsExist("info")
    //fmt.Println(flag)
    // 删除 es 索引
    //flag := es.DelIndex("info")
    //fmt.Println(flag)
    // 查询 es 单条数据
    //str := es.GetData("es_index_yunduan_district", "93a1e9c6b5c059ef322778a674f16a36")
    //fmt.Println(str)
    // 刷新 es 数据
    //flag := es.FlushEs("es_index_yunduan_district")
    //fmt.Println(flag)
    // 查询 es 数据列表
    //where := make(map[string]map[string]interface{})
    //t1 := make(map[string]interface{})
    //t2 := make(map[string]interface{})
    //t1["name|code"] = "湖北省"
    //t2["code"] = "110000"
    //where["or"] = t1
    //where["="] = t2
    //sort := make(map[string]interface{})
    //sort["is_star"] = "desc"
    //sort["name_letter"] = "asc"
    //list, count := es.GetList("es_index_yunduan_user_friend", nil, 1, 10, nil)
    //if count > 0 {
    //    jsonStr, _ := json.Marshal(list)
    //    fmt.Println(string(jsonStr))
    //    fmt.Println(count)
    //}
    //data := map[string]interface{}{"is_star": 0}
    //res, err := es.Modify("es_index_yunduan_user_friend", "1fb7d7a916632f31905ba43574bdb547", data)
    //fmt.Println(res, err)
    //res, err := es.Add("ttt", "111", data)
    //fmt.Println(res, err)
    //res, err := es.Delete("ttt", "111")
    //fmt.Println(res, err)
    //endregion

    //region 查找最长不重复子串
    //str := "工要在地一上是中国在我人有的和主产不为这工要经以发了中国"
    //newStr := ""
    //lastOcurred := make(map[rune]int)
    //start := 0
    //index := 0
    //last := 0
    //maxLen := 0
    //for i, ch := range []rune(str) {
    //    if lastOcurred[ch] > start {
    //        start = lastOcurred[ch]
    //    }
    //    if i-start > maxLen {
    //        maxLen = i - start
    //        index = start + 1
    //        last = i + 1
    //    }
    //    newStr = string([]rune(str)[index:last])
    //    lastOcurred[ch] = i
    //}
    //fmt.Println(maxLen, newStr)
    //endregion

    //region 时间、日期操作
    //nn := t.Now()
    //fmt.Println(nn)
    //fmt.Println(nn.NewFormat("Y-m-d H:i:s"))
    //fmt.Println(time.Now())
    //now := time.Now() //获取当前时间
    //fmt.Println(now)
    //fmt.Printf("type=%T val=%v", now, now)
    //fmt.Println(strconv.Itoa(now.Year()) + "年" + now.Month().String() + "月" + strconv.Itoa(now.Day()))
    //fmt.Println(now.Local())
    //fmt.Println(strings.Split(now.String(), ".")[0])
    //fmt.Println(strings.Fields(now.String())[0])
    //fmt.Println(now.NewFormat("Y-m-d H:i:s"))
    //fmt.Println(now.Format("2006-01-02 15:04:05"))
    //time.Sleep(100)
    //endregion

    //region 封装操作数据库
    //var u = adminUserLog.AdminUserLog{
    //    Data:      nil,
    //    Where:     nil,
    //    List:      nil,
    //    Fields:    []string{"id", "username", "login_time", "login_ip", "action", "log", "ctime"},
    //    Limit:     "10,1",
    //    Order:     "ctime",
    //    OrderType: "desc",
    //}
    //res, err := u.GetModel()
    //if err != nil {
    //    panic(err)
    //}
    //num := data.GetRandomStr(8)
    //fmt.Println(num)
    //return
    //arr := make([]map[string]interface{}, 0)
    //arr1 := make(map[string]interface{})
    //arr1["id"] = data.Getmd5("yunduan_admin_user_log$"+string(time.Now().UnixMicro())+data.GetRandomNum(8), "")
    //arr1["username"] = "adminBBB"
    //arr1["login_time"] = time.Now().Unix()
    //arr1["login_ip"] = "127.0.0.1"
    //arr1["action"] = "login"
    //arr1["log"] = "登录222"
    //arr1["ctime"] = time.Now().Unix()
    //arr = append(arr, arr1)
    //arr2 := make(map[string]interface{})
    //arr2["id"] = data.Getmd5("yunduan_admin_user_log$"+string(time.Now().UnixMicro())+data.GetRandomNum(9), "")
    //arr2["username"] = "admin3334455"
    //arr2["login_time"] = time.Now().Unix()
    //arr2["login_ip"] = "127.0.0.1"
    //arr2["action"] = "login"
    //arr2["log"] = "登录333"
    //arr2["ctime"] = time.Now().Unix()
    //arr = append(arr, arr2)
    //where := make(map[string]map[string]interface{})
    //w := make(map[string]interface{})
    //w2 := make(map[string]interface{})
    //w["id"] = []string{"ed1ef8f920ed494510141a9d7da1916e", "6252c48b788ca4984acc75774e6f7a9a"}
    //w["id"] = "ed1ef8f920ed494510141a9d7da1916e"
    //w["username"] = "admin"
    //w2["username"] = "admin"
    //where["or"] = w
    //where["="] = w2
    //var ul = adminUserLog.AdminUserLog{
    //    //Data: arr1,
    //    Where: where,
    //    //List:      arr,
    //    Fields:    []string{"id", "username"},
    //    Order:     "",
    //    OrderType: "",
    //    Limit:     "",
    //    //Sql:       "select * from yunduan_admin_user_log order by ctime desc limit 2",
    //    //Sql: "update yunduan_admin_user_log set `log`='adminTTT' where `id`='2fa96e6d7dd46027eefa2501d25c56c8'",
    //}
    //res, err := ul.GetModel()
    //if err != nil {
    //    fmt.Println(err)
    //    return
    //}
    //id := ""
    //username := ""
    //res.Scan(&id, &username)
    //fmt.Println(id, username)
    //fmt.Println(res)
    //str := data.Array2Json(res)
    //fmt.Println(str)
    //endregion

    //region 字符串时间、时间辍相互转换
    //t := "2022-07-19 17:24:33"
    //fmt.Println(time.Unix(1657698012, 0).NewFormat("Y-m-d H:i:s"))
    //fmt.Println(data.GetUnixTime(t))
    //endregion
}
